﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Sendz.Domain;
using Sendz.Model;
using Ninject;
using Sendz.WebUI.Infrastructure;
using System.Configuration;
using System.Web.Security;
using Sendz.WebUI.Authorization;

namespace Sendz.WebUI.Helpers
{
    public class UserHelpers
    {
        #region Repository injection //////////////////////////////////

        // Aggregate repositories, injected by container factory in /Infrastructure
        // Use alternate injection pattern with NinjectHelper (see notes in NinjectControllerFactory)
        private IUserRepository dbUser;

        public UserHelpers()
        {
            NinjectHelper.Kernel.Inject(this);
        }

        [Inject]
        public IUserRepository Repository 
        { 
            set
            {
                dbUser = value;
            }
        }

        #endregion

        public string ResetPassword(User user, string password)
        {
            dbUser.ResetUserPassword(user.UserKey, password);
            var emailHelperer = new EmailHelpers();

            if (emailHelperer.SendEmail(ConfigurationManager.AppSettings.Get("FromAddressAdmin"), user.Email, "Your password has been reset!", "Your password has been reset to: " + password + "<br />Please visit www.getfrontdesk.com to login."))
                return "An email has been sent containing the new password to the email on record";
            else
                return "Sorry, the password could not be emailed right now, please try again later";
        }
    }

    public class UserInfoAttribute : ActionFilterAttribute
    {
        #region Repository injection //////////////////////////////////

        // Aggregate repositories, injected by container factory in /Infrastructure
        // Use alternate injection pattern with NinjectHelper (see notes in NinjectControllerFactory)
        private IUserRepository dbUser;

        public UserInfoAttribute()
        {
            NinjectHelper.Kernel.Inject(this);
        }

        [Inject]
        public IUserRepository Repository 
        { 
            set
            {
                dbUser = value;
            }
        }

        #endregion

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            var user = Membership.GetUser(true) as CustomMembershipUser;
            if (user != null)
            {
                var userRecord = dbUser.GetUserByKey((Guid)user.ProviderUserKey);
                filterContext.Controller.ViewData["UserFirstname"] = userRecord.Firstname;
                filterContext.Controller.ViewData["UserInvitesRemaining"] = userRecord.InvitesRemaining;
                filterContext.Controller.ViewData["UserResponsesRemaining"] = userRecord.ResponsesRemaining;
            }
        }
    }
}